package com.afinal.intelligencetrash.ui.base;

import android.content.res.Resources;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.afinal.intelligencetrash.R;
import com.afinal.intelligencetrash.utils.DebugLog;
import com.afinal.intelligencetrash.utils.LogUtil;
import com.afinal.intelligencetrash.utils.SimpleUtil;


/**
 * Created by cyy on 17-10-7.
 */

public abstract class AbstractBaseCoreActivity extends AppCompatActivity implements View.OnClickListener{
    public static final String LOGTAG = LogUtil.makeLogTag(AbstractBaseCoreActivity.class);
    private Toolbar toolbar;
    private TextView toolbarTitle;

    /**
    * 如果在启动该Activity前需要进行检查,重写该函数,返回true,则启动,否则退出.
    * */
    protected boolean launchCheck(){
        return true;
    }
    protected abstract void initView();

    protected abstract void initData();

    protected abstract int getLayoutResId();

    protected abstract String getToolbarTitle();

    protected int getNavToolbarLayoutResId() {
        return R.layout.navigation_toolbar;
    }

    /**
     * 是否显示返回键
     * @return
     */
    abstract protected boolean isDisplayHomeAsUpEnabled();

    /*
    * 该方法在onDestroy()时
    * */
    abstract protected void dealloc();


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        DebugLog.d(DebugLog.MY_TAG, "OnCreate");
        if(!launchCheck()){
            finish();
            return;
        }
        initBase();
    }

    private View parentView;
    private int statusBarHeight, toolbarHeight;

    private void initBase(){
        if (getNavToolbarLayoutResId() != 0) {
            initContentView();
        }
        setContentView(getLayoutResId());//把设置布局文件的操作交给继承的子类
        ViewGroup contentFrameLayout = (ViewGroup) findViewById(Window.ID_ANDROID_CONTENT);
        parentView = contentFrameLayout.getChildAt(0);   //ContentParent的第一个直接子View,
        if (parentView != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
//            Boolean internal attribute to adjust view layout based on system windows such as
// the status bar. If true, adjusts the padding of this view to leave space for the system windows.
// Will only take effect if this view is in a non-embedded activity.
            parentView.setFitsSystemWindows(false);
        }
        toolbar = (Toolbar) this.findViewById(R.id.toolbar);

        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        if (isDisplayHomeAsUpEnabled()) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setDisplayShowHomeEnabled(true);
            getSupportActionBar().setHomeAsUpIndicator(getToolbarHomeAsUpIcon());
        }
        toolbarTitle = (TextView) toolbar.findViewById(R.id.nav_toolbar_title);
        toolbarTitle.setText(getToolbarTitle());
        toolbarTitle.setOnClickListener(this);

        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                OnToolbarHomeAsUpClick(v);
            }
        });


        initSystemBar();

        statusBarHeight = getInternalDimensionSize(getResources(), "status_bar_height");
        toolbarHeight = toolbar.getLayoutParams().height;


        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) {//calculate Toolbar Height
            parentView.setFitsSystemWindows(false);
            setToolbarHeight();
        } else {
            parentView.setFitsSystemWindows(true);
        }
        initView();

        initData();
    }

    protected void OnToolbarHomeAsUpClick(View v) {
        onBackPressed();
    }

    protected int getToolbarHomeAsUpIcon() {
        return R.drawable.base_nav_back_icon;
    }

    @Override
    public void onClick(View v) {

    }

    private LinearLayout contentLayout;  //ContentParent的直接子ViewGroup

    protected void initContentView() {
        ViewGroup content = (ViewGroup) findViewById(android.R.id.content);
        content.removeAllViews();
        contentLayout = new LinearLayout(this);
        contentLayout.setOrientation(LinearLayout.VERTICAL);
        content.addView(contentLayout);
        LayoutInflater.from(this).inflate(getNavToolbarLayoutResId(), contentLayout, true);
    }

    @Override
    public void setContentView(@LayoutRes int layoutResID) {
        if (getNavToolbarLayoutResId() == 0) {//default
            super.setContentView(layoutResID);
        } else {
            LayoutInflater.from(this).inflate(layoutResID, contentLayout, true);
        }
    }

    protected void initSystemBar() {
        Window win = getWindow();
        WindowManager.LayoutParams winParams = win.getAttributes();
        final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
        winParams.flags |= bits;
        win.setAttributes(winParams);

        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            win.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            win.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            win.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            win.setStatusBarColor(Color.TRANSPARENT);
        }
    }

    protected void setToolbarTitle(String text){
        if(toolbarTitle != null){
            toolbarTitle.setText(text);
        }
    }
    private void setToolbarHeight() {
        toolbar.getLayoutParams().height = statusBarHeight + toolbarHeight;
        toolbar.setPadding(0, statusBarHeight, 0, 0);

    }

    protected void setStatusBarHide() {
        if (Build.VERSION.SDK_INT < 16) {//before Jelly Bean Versions{
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
        } else { // Jelly Bean and up

            View decorView = getWindow().getDecorView();
            // Hide the status bar.
            int ui = View.SYSTEM_UI_FLAG_FULLSCREEN;
            decorView.setSystemUiVisibility(ui);

            //Hide actionbar
//            ActionBar actionBar = getActionBar();
//            actionBar.hide();
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        dealloc();
    }

    private int getInternalDimensionSize(Resources res, String key) {
        int result = 0;
        int resourceId = res.getIdentifier(key, "dimen", "android");
        if (resourceId > 0) {
            result = res.getDimensionPixelSize(resourceId);
        }
        return result;
    }


}
